package cn.luoyifan;

import org.elasticsearch.action.delete.DeleteRequestBuilder;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexRequestBuilder;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.update.UpdateRequestBuilder;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.sort.SortOrder;
import org.elasticsearch.transport.client.PreBuiltTransportClient;
import org.junit.Test;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;

public class ESTest {

    //连接ES

    private TransportClient getClient(){
        //设置
        Settings settings = Settings.builder()
                //嗅探
                .put("client.transport.sniff", true).build();
        TransportClient client = null;
        try {
            //ES客户端
             client = new PreBuiltTransportClient(settings)
                    //一个ES的主机
                    .addTransportAddress(new InetSocketTransportAddress(InetAddress.getByName("127.0.0.1"), 9300));
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return client;
    }


    //CRUD
    @Test
    public void index()throws UnknownHostException{
        //获取连接
        TransportClient client = getClient();
        //创建索引
        IndexRequestBuilder indexRequestBuilder = client.prepareIndex("pethome", "user", "1");
        //添加文档
        Map<String,Object> data = new HashMap<>();
        data.put("id",1L);
        data.put("username","hg");
        data.put("realname","哈哥");
        data.put("sex",1);
        data.put("age",18);
        IndexResponse indexResponse = indexRequestBuilder.setSource(data).get();
        System.out.println(indexResponse);
    }

    @Test
    public void get(){
        //获取连接
        TransportClient client = getClient();
        //获取
        GetRequestBuilder getRequestBuilder = client.prepareGet("pethome", "user", "1");
        //结果
        GetResponse getResponse = getRequestBuilder.get();
        System.out.println(getResponse.getSource());
    }

    @Test
    //局部修改
    public void update() {
        //获取连接
        TransportClient client = getClient();
        //修改
        UpdateRequestBuilder updateRequestBuilder = client.prepareUpdate("pethome", "user", "1");
        Map<String, Object> data = new HashMap<>();
        data.put("id", 1L);
        data.put("username", "wg");
        data.put("realname", "万狗");
        data.put("sex", 1);
        updateRequestBuilder.setDoc(data);
        UpdateResponse updateResponse = updateRequestBuilder.setDoc(data).get();
        System.out.println(updateResponse);
    }

    @Test
    public void delete() {
        TransportClient client = getClient();
        //删除
        DeleteRequestBuilder deleteRequestBuilder = client.prepareDelete("pethome", "user", "1");
        //删除结果
        DeleteResponse deleteResponse = deleteRequestBuilder.get();
        System.out.println(deleteResponse);
    }

    //综合查询
    @Test
    public void search() {
        TransportClient client = getClient();
        //指定搜索的索引库
        SearchRequestBuilder searchBuilder = client.prepareSearch("pehome");
        //排序    倒序
        searchBuilder.addSort("age", SortOrder.DESC);
        //分页
        searchBuilder.setFrom(1);
        searchBuilder.setSize(10);

        //查询条件
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();

        boolQueryBuilder.must(QueryBuilders.matchQuery("username","wg hg"));

        boolQueryBuilder
                .filter(QueryBuilders.termQuery("sex",1))
                .filter(QueryBuilders.rangeQuery("age").gte(10).lte(50));

        //获取结果
        SearchResponse searchResponse = searchBuilder.setQuery(boolQueryBuilder).get();

        //命中,查询到的结果
        SearchHits hits = searchResponse.getHits();

        //获取总的命中数
        long totalHits = hits.getTotalHits();
        System.out.println("查询到的总条数" + totalHits);

        //结果列表
        SearchHit[] list = hits.getHits();
        for (SearchHit searchHit : list){
            //拿到数据
            Map<String, Object> source = searchHit.getSource();
            System.out.println(source);
        }

    }

}
